﻿$(function () {
    $("#submit").click(function () {
        Submit();
    });

    $("#body").keypress(function (event) {
        if (event.which == '13') {
            Submit();
        }
    });

    $("#IsMaster").change(function () {
        ShowHideMaster();
    });

    $("#InheritMaster").change(function () {
        ShowHideMaster();
    });

    ShowHideMaster();
});

function ShowHideMaster() {
    var IsCurrentMaster = $("#IsCurrentMaster").is(":checked");
    var IsMasterAssigned = $("#IsMasterAssigned").is(":checked");
    var IsMaster = $("#IsMaster").is(":checked");
    var InheritMaster = $("#InheritMaster").is(":checked");
    var showmaster = false;
    var showinherit = false;

    if (IsMasterAssigned && !IsCurrentMaster) {
        showmaster = false;
    } else if (IsMasterAssigned && IsCurrentMaster && !InheritMaster) {
        showmaster = true;
    } else if (IsMasterAssigned && IsCurrentMaster && InheritMaster) {
        showmaster = false;
    } else if (!IsMasterAssigned && InheritMaster) {
        showmaster = false;
    } else if (!IsMasterAssigned && !InheritMaster) {
        showmaster = true;
    }

    if (IsMaster) {
        showinherit = false;
    } else {
        showinherit = true;
    }

    if (showmaster) {
        $("#IsMasterdiv").show();
    } else {
        $("#IsMaster").attr("checked", false);
        $("#IsMasterdiv").hide();
    }

    if (showinherit) {
        $("#InheritMasterdiv").show();
    } else {
        $("#InheritMaster").attr("checked", false);
        $("#InheritMasterdiv").hide();
    }
}

function Submit() {

    $("#submit").hide();

    var input = {
        ID: $("#id").val(),
        IsMaster: $("#IsMaster").is(":checked"),
        InheritMaster: $("#InheritMaster").is(":checked"),
        Name: $("#Name").val(),
        Street: $("#Street").val(),
        Street2: $("#Street2").val(),
        City: $("#City").val(),
        State: $("#State").val(),
        ZipCode: $("#ZipCode").val(),
        Latitude: $("#Latitude").val(),
        Longitude: $("#Longitude").val(),
        TimeZone: $("#TimeZone").val(),
        DisplayOrder: $("#DisplayOrder").val(),
        LongNote: $("#LongNote").val()
    };

    Ajax({
        url: "/DailyPrayers/SaveLocation",
        data: JSON.stringify(input),
        success: function (data) {
            if (data.IsValid == false) {
                ShowValidationErrors(data);
            } else if (data.IsSaved == true) {
                location.href = "/DailyPrayers?msg=1";
            }
        },
        complete: function () {
            $("#submit").show();
        }
    });
}

$(function () {
    $('#loadgeo').click(function () {
        $('#loadgeo').val("getting geo location....");
        var address = $("#Street").val() + ' '
                    + $("#City").val() + ' '
                    + $("#State").val() + ' '
                    + $("#ZipCode").val();
        geocoder = new GClientGeocoder();
        geocoder.getLocations(address, ShowGeo);
    });
});
        
function ShowGeo(response) {
    if (!response || response.Status.code != 200) {
        alert("Sorry, we were unable to geocode that address");
    } 
    else 
    {
        place = response.Placemark[0];
        $("#Latitude").val(place.Point.coordinates[1]);
        $("#Longitude").val(place.Point.coordinates[0]);
        $('#loadgeo').val("got it!");
        setTimeout(function () {
            $('#loadgeo').val("load from address again");
        }, 3000)
    }
}